const express = require('express')
const router = express.Router()

// 自定义库
const db = require('../config/database.js')
const code = require('../config/code.js')
const PPT = require('../model/ppt.js')
const { makeUpdateSql, formatTime }  = require('../utils/index.js')

router.post('/add', (req, res, next) => {
  (async () => {
    const { title, intro, cover, tag } = req.body
    // 以后可以在数据库里抽取模板加入，这里可以传入模板id
    const content = '[{"version": "5.2.1","objects": [],"background": "#FFFFFFFF"}]'
    const errTips = {
      title: '请输入文档名称',
      tag: '请选择标签'
    }
    for (const x in errTips) {
      if (!req.body[x]) {
        return res.json({code: code.paraErr, msg: errTips[x]})
      }
    }
    const [errAdd, resAdd] = await db.query(PPT.addPPT, [req.auth.id, title, intro, content, cover, tag])
    if (errAdd) {
      return res.json({code: code.dbErr, msg: '添加失败'})
    }
    if (resAdd.affectedRows === 0) {
      return res.json({code: code.dbErr, msg: '添加失败'})
    }
    return res.json({
      code: code.ok,
      msg: '添加成功',
      data: {
        id: resAdd.insertId
      }
    })
  })()
})

router.get('/delete', (req, res, next) => {
  (async () => {
    const { id } = req.query
    if (!id) {
      return res.json({code: code.paraErr, msg: '缺少参数'})
    }
    const [errDel, resDel] = await db.query(PPT.delPPT, [id, req.auth.id])
    if (errDel) {
      return res.json({code: code.dbErr, msg: '删除失败'})
    }
    if(resDel.affectedRows === 0) {
      return res.json({code: code.dbErr, msg: '删除失败'})
    }
    return res.json({
      code: code.ok,
      msg: '删除成功',
      data: {
        id: resDel.insertId
      }
    })
  })()
})

router.get('/getone', (req, res, next) => {
  (async () => {
    const { id } = req.query
    if (!id) {
      return res.json({code: code.paraErr, msg: '参数不足'})
    }
    const [err, results] = await db.query(PPT.findPPTById, [req.auth.id, id])
    if (err) {
      return res.json({code: code.dbErr, msg: '获取失败'})
    }
    if (results.length === 0) {
      return res.json({code: code.err, msg: '获取失败'})
    }
    return res.json({
      code: code.ok,
      msg: '获取成功',
      data: results[0]
    })
  })()
})

router.get('/get', (req, res, next) => {
  (async () => {
    let { title, tag, pageNum=0, pageSize=10 } = req.query
    pageNum = parseInt(pageNum)
    pageSize = parseInt(pageSize)
    if (!title) {
      title = '.'
    }
    if (!tag) {
      tag = '@@@***###$$$&&&leazerfspace20220509' // 这里是为了判断是否查询全部tag的特殊标记
    }
    const [errTotal, resTotal] = await db.query(PPT.findPPT, [req.auth.id, title, `"${tag}"`, tag])
    if (errTotal) {
      return res.json({code: code.dbErr, msg: '获取失败'})
    }
    const [err, results] = await db.query(PPT.findPPTPage, [req.auth.id, title, `"${tag}"`, tag, (pageNum-1)*pageSize, pageSize])
    // console.log(mysql.format(PPT.findPPTPage, [req.auth.id, title, `"${tag}"`, pageNum*pageSize, pageSize]))
    if (err) {
      return res.json({code: code.dbErr, msg: '获取失败'})
    }
    return res.json({
      code: code.ok,
      msg: '获取成功',
      data: {
        pptList: results,
        total: resTotal[0].total
      }
    })
  })()
})

router.post('/update', (req, res, next) => {
  (async () => {
    let { id, title, intro, content, cover, tag } = req.body
    if (!id) {
      return res.json({code: code.paraErr, msg: '参数不足'})
    }
    const { sql, params } =  makeUpdateSql(['title', 'intro', 'content', 'cover', 'tag'], req.body, 'ppt', id, req.auth.id)
    const [errUpdate, results] = await db.query(sql, params)
    if (errUpdate) {
      return res.json({code: code.dbErr, msg: '更新失败'})
    }
    if (results.affectedRows === 0) {
      return res.json({code: code.dbErr, msg: '更新失败'})
    }
    res.json({
      code: code.ok,
      msg: '更新成功',
      data: {
        id: id,
        update_time: formatTime()
      }
    })
  })()
})



module.exports = router
